package com.xuexiang.xuidemo.page.base;

import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.TextTool;
import ohos.app.Context;

import com.xuexiang.xuidemo.ResourceTable;

import java.util.List;

/**
 * 单文字PageList
 *
 * @param <E> Item对象泛型
 * @since 2021-03-25
 */
public abstract class SimpleTextListAbilitySlice<E extends SimpleTextListAbilitySlice.SimpleText>
    extends BasePageAbilitySlice implements BaseProvider.OnItemClickEvent<E> {
    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_simple_list;
    }

    @Override
    protected void onActive() {
        super.onActive();
        ListContainer listContainer = findCastComponentById(ResourceTable.Id_list);
        ItemProvider<E> itemProvider = new ItemProvider<>(getContext(), getDatas());
        listContainer.setItemProvider(itemProvider);
        itemProvider.setOnItemClickEvent(this);
    }

    /**
     * 返回列表对应的数据
     *
     * @return Provider.Datas
     */
    protected abstract List<E> getDatas();

    /**
     * Item点击事件回调, 子类重写该方法以实现点击事件
     *
     * @param component Item最外层组件
     * @param position 数据的索引位置
     * @param data 数据
     */
    public abstract void onItemClick(Component component, int position, E data);

    /**
     * 列表Provider
     *
     * @param <E> Item对象泛型
     * @since 2021-03-31
     */
    private static class ItemProvider<E extends SimpleTextListAbilitySlice.SimpleText> extends BaseProvider<E> {
        private ItemProvider(Context context, List<E> datas) {
            super(context, datas);
        }

        @Override
        public BaseHolder<E> onCreateItemHolder(Context context, int position) {
            return new ItemHolder<>(context);
        }
    }

    /**
     * 列表Text ItemHolder
     *
     * @param <E> Item对象泛型
     * @since 2021-03-31
     */
    private static class ItemHolder<E extends SimpleTextListAbilitySlice.SimpleText> extends BaseHolder<E> {
        private final Text titleTv;
        private final Text descTv;

        private ItemHolder(Context context) {
            super(context, ResourceTable.Layout_provider_item_text);
            titleTv = findCastComponentById(ResourceTable.Id_tv_title);
            descTv = findCastComponentById(ResourceTable.Id_tv_desc);
        }

        @Override
        public void onBindComponent(int position, E data) {
            titleTv.setText(data.getName());
            if (!TextTool.isNullOrEmpty(data.getDescribe())) {
                descTv.setText(data.getDescribe());
                descTv.setVisibility(Component.VISIBLE);
            } else {
                descTv.setVisibility(Component.HIDE);
            }
        }
    }

    /**
     * 列表Text抽象接口
     *
     * @since 2021-03-31
     */
    public interface SimpleText {
        /**
         * 列表Item的文字
         *
         * @return 返回Item.name
         */
        String getName();

        /**
         * 描述信息
         *
         * @return 描述
         */
        default String getDescribe() {
            return null;
        }
    }
}
